package yxy.game.pm2.active.module.rank;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.ActiveModule_冲榜;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.rank.active.RankRushEnum;
import yxy.game.pm2.bean.module.rank.active.RankRushHandle;

public class ActiveRushModule extends AreaModule {
	
	// bean===========================================================================
	
	//整体活动部分
	private int runday;
	private int rankrushtype;
	private boolean running;	// 是否运行
	
	//排行奖励部分
	private int rewardtype; 	// 等待发奖的类型
	private DateTime rewardtime;	//奖励发放时间
	private boolean award;		// 是否发放了奖励

	public int getRunday() {
		return runday;
	}
	public void setRunday(int runday) {
		this.runday = runday;
	}
	public int getRankrushtype() {
		return rankrushtype;
	}
	public void setRankrushtype(int rankrushtype) {
		this.rankrushtype = rankrushtype;
	}
	public boolean isRunning() {
		return running;
	}
	public void setRunning(boolean running) {
		this.running = running;
	}
	public int getRewardtype() {
		return rewardtype;
	}
	public void setRewardtype(int rewardtype) {
		this.rewardtype = rewardtype;
	}
	public DateTime getRewardtime() {
		return rewardtime;
	}	
	public void setRewardtime(DateTime rewardtime) {
		this.rewardtime = rewardtime;
	}
	public boolean isAward() {
		return award;
	}
	public void setAward(boolean award) {
		this.award = award;
	}
	
	// func===========================================================================

	public void update(activeControl sai) {
		if(sai == null) {
			return;
		}
		
		//
		if(!sai.running() && isRunning()) {
			setRunning(false);
			set();
		}
		
		//活动控制开启 排行奖励发放关闭
		if(sai.running() && !isRunning()) {
			setRunning(true);
//			setRewardtime(null);
			set();
			
			//删除上个排行榜
			RankRushHandle.delete(getAreaId(), getRankrushtype());
		}
		
		if(isRunning()) {
			if(getRewardtype() == 0) {
				setRewardtype(getRankrushtype());
				set();
			}
			
			int newrunday = getrunday(sai.getTimeline().getStartTime());
			int oldrunday = getRunday();
			if(newrunday != oldrunday) {
				setRunday(newrunday);
				int newruntype = getruntype(newrunday);
				int oldruntype = getRankrushtype();
				
				if(newruntype != oldruntype) {
					setRankrushtype(newruntype);
					//删除上个排行榜
					RankRushHandle.delete(getAreaId(), newruntype);
				}
		
				set();
			}
			
			if(isAward() && getRewardtype() != getRankrushtype()) {
//				删除上个排行榜
//				RankRushHandle.delete(getAreaId(), getRewardtype());
				
				setRewardtype(getRankrushtype());
				setRewardtime(getrunendtime(getRunday()));
				setAward(false);
				set();
			}
			
		}
		
//		if(!sai.getEndtime().equals(getRewardtime())) {
//			setRewardtime(sai.getEndtime());
//			set();
//		}
	}

	/**
	 * 获取循环内运行天数
	 * @param start
	 * @return
	 */
	private static int getrunday(DateTime start) {
		int days = ActiveUtil.days(start, DateTime.now());
		int runday = days % 28 + 1;
		return runday;
	}
	
	/**
	 * 获取运行冲榜类型
	 * @param runday 循环内运行天数
	 * @return
	 */
	public static int getruntype(int runday){
		if(runday >= 1 && runday <= 7) {
			return RankRushEnum.冲榜_金币兑换.getType();
		}
		
		if(runday >= 8 && runday <= 14) {
			return RankRushEnum.冲榜_副本关卡.getType();
		}
		
		if(runday >= 15 && runday <= 21) {
			return RankRushEnum.冲榜_剿匪次数.getType();
		}
		
		if(runday >= 22 && runday <= 28) {
			return RankRushEnum.冲榜_快速作战.getType();
		}
		
		return 0;
	}
	
	/**
	 * 计算单个冲榜结束时间
	 * @param runday
	 * @return
	 */
	public static DateTime getrunendtime(int runday){
		DateTime now = DateTime.now();
		int yu = runday % 7;
		if(yu == 0) {
			DateTime end = now.withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59).withMillisOfSecond(0);
			return end;	
		}
		int surplusdays = 7 - yu;
		DateTime end = now.plusDays(surplusdays).withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59).withMillisOfSecond(0);
		return end;
	}
	
	// redis==========================================================================

	static private String key(int areaId, int rankrushtype) {
		return key(areaId, "RUSHREWARD:" + rankrushtype);
	}

	public void set() {
		try {
			setv(key(getAreaId(), ActiveModule_冲榜.acttype), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	
	public static ActiveRushModule get(String playerCode) {
		AreaBean area = area(playerCode);
		return get(area.getAreaId());
	}

	public static ActiveRushModule get(AreaBean area) {
		return get(area.getAreaId());
	}
	
	public static ActiveRushModule get(int areaId) {
		try {
			ActiveRushModule module = getv(key(areaId, ActiveModule_冲榜.acttype));
			activeControl sai = activeControl.get(areaId, ActiveModule_冲榜.acttype);
			if (module == null) {
				module = create(areaId, sai);
			}
			module.update(sai);
			return module;
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static ActiveRushModule create(int areaId, activeControl sai) {
		if(sai == null) {
			return null;
		}
		
		int runday = getrunday(sai.getStarttime());
		int runtype = getruntype(runday);
		DateTime endtime = getrunendtime(runday);
		
		if(endtime.isAfter(sai.getEndtime())) {
			endtime = sai.getEndtime();
		}
		
		ActiveRushModule module = new ActiveRushModule();
		module.setRunday(runday);
		module.setAreaId(areaId);
		module.setRankrushtype(runtype);
		
		module.setRewardtype(runtype);
		module.setRunning(true);
		module.setRewardtime(endtime);
		module.setAward(false);
		module.set();
		
		return module;
	}
	
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getAreaId(), getRankrushtype()));
	}

}
